package com.example.administrator.myapplication;

import android.accessibilityservice.AccessibilityService;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewParent;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import java.util.List;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn;
    private Button homeBtn;
    private Button baiduBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.gotofuzhu);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        homeBtn = findViewById(R.id.homeBtn);
        homeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
            }
        });
        baiduBtn = findViewById(R.id.baiduBtn);
        baiduBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
        Button gotoSec=findViewById(R.id.gotoSec);

        gotoSec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*书写方法一
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName(MainActivity.this, Main2Activity.class);
                intent.setComponent(componentName);
                startActivity(intent);
                */
                /**
                 * 书写方法2
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                 */
                /**
                 * 书写方法3也是最简单 的启动方法
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                 */
                /**
                 * 第三种方法，使用ActionName直接调用
                 */
                Intent intent = new Intent();
                intent.setAction("cn.boz.action.bozAction");
                startActivity(intent);

            }
        });

        List<String> collect = Stream.<String>of("cn.boz.myCategorySecond", "cn.boz.myCategoryThird").collect(Collectors.toList());
        ArrayAdapter<String> arrayAdapter;
        //注意这个资源ID必须是根节点是TextView
        arrayAdapter = new ArrayAdapter<String>(MainActivity.this,R.layout.simple_spinner_item,collect);

        Spinner spinner=findViewById(R.id.am1_sp_sel);
        spinner.setAdapter(arrayAdapter);


        Button am1_bt_goto=findViewById(R.id.am1_bt_goto);
        am1_bt_goto.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View view) {
                System.out.println(view);
                String item= (String) spinner.getSelectedItem();
                System.out.println(item);
                if(item!=null&&item.length()>0){
                    final Intent intent = new Intent();
                    intent.setAction("cn.boz.action.bozAction");
                    Set<String> categories = intent.getCategories();
                    if(categories!=null){
                        categories.forEach(it->{
                            intent.removeCategory(it);
                        });
                    }
                    intent.addCategory(item);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        
    }
}
